USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0002_DIAS_PRIM]    Script Date: 11/24/2011 14:32:04 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WASP_M0002_DIAS_PRIM]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[WASP_M0002_DIAS_PRIM]
GO

USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0002_DIAS_PRIM]    Script Date: 11/24/2011 14:32:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[WASP_M0002_DIAS_PRIM]
AS
BEGIN
	SELECT	cre.aosuc AS suc
			,cre.aocta AS cta
			,cre.aooper AS oper,
			DATEDIFF(DAY, cre.aofval, MIN(cuot.ppfpag)) AS diasprim
	INTO	diasprim
	FROM	fsd010 AS cre LEFT JOIN fsd601 AS cuot ON cre.aosuc = cuot.ppsuc AND cre.aocta = cuot.ppcta AND cre.aooper = cuot.ppoper
	WHERE 	cre.aosbop = 0
	AND		cuot.d601co = 'S'
	AND		cre.aotasa <> 0
	GROUP BY cre.aosuc,cre.aocta,cre.aooper,cre.aofval
END

GO


